package com.fsxgt.datagrid.ds.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.page.PageParam;
import com.fsxgt.datagrid.ds.bo.DatasourceJdbcBO;
import com.fsxgt.datagrid.ds.exception.DsException;
import com.fsxgt.datagrid.ds.service.impl.DatasourceJdbcService;
import com.fsxgt.datagrid.ds.utils.JdbcConstants;
import com.fsxgt.datagrid.ds.utils.ReturnT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "数据源接口", value = "DatasourceController")
@RequestMapping(produces = "application/json; charset=UTF-8",path = "/datasourceJdbc")
@RestController
public class DatasourceJdbcController extends BaseController {

	@Autowired
	private DatasourceJdbcService datasourceJdbcService;


	@ApiOperation(hidden = true, value = "添加数据源", notes = "")
	@RequestMapping(path = "/add", method = RequestMethod.POST)
	public ReturnT add(@RequestBody DatasourceJdbcBO datasourceJdbcBO, HttpServletRequest request) throws Exception {


		datasourceJdbcService.addDatasource(datasourceJdbcBO);
		
		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "删除数据源", notes = "")
	@RequestMapping(path = "/delete", method = RequestMethod.POST)
	public ReturnT delete(@RequestBody DatasourceJdbcBO datasourceJdbcBO, HttpServletRequest request) throws DataGridException {



		datasourceJdbcService.deleteDatasource(datasourceJdbcBO);
		
		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "修改数据源", notes = "")
	@RequestMapping(path = "/modify", method = RequestMethod.POST)
	public ReturnT modify(@RequestBody DatasourceJdbcBO datasourceJdbcBO, HttpServletRequest request) throws DataGridException {



		datasourceJdbcService.modifyDatasource(datasourceJdbcBO);
		
		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "获取数据源", notes = "")
	@RequestMapping(path = "/getById", method = RequestMethod.POST)
    public ReturnT getById(@RequestBody DatasourceJdbcBO datasourceJdbcBO) {

		DatasourceJdbcBO bo=datasourceJdbcService.getDatasourceById(datasourceJdbcBO.getId());
		
        return new ReturnT(bo);
    }
	
	
	@ApiOperation(hidden = true, value = "分页查询", notes = "")
	@RequestMapping(path = "/findPage", method = RequestMethod.POST)
    public ReturnT find(@RequestBody PageParam<DatasourceJdbcBO> pageParam) {
		
		PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
		
		List list=datasourceJdbcService.findDatasource(pageParam.getParam());
		
		PageInfo<DatasourceJdbcBO> pageInfo = new PageInfo<>(list);
		
		
        return new ReturnT(pageInfo);
    }
	
	@ApiOperation(hidden = true, value = "获取字典", notes = "")
	@RequestMapping(path = "/getDic", method = RequestMethod.POST)
    public ReturnT find(@RequestBody String s) {
		
		Map result=new HashMap();
		Map database=null;
		
		database=new HashMap();
		database.put("url", JdbcConstants.MYSQL_URL);
		database.put("driver", JdbcConstants.MYSQL_DRIVER);
		result.put(JdbcConstants.MYSQL, database);
		
		database=new HashMap();
		database.put("url", JdbcConstants.CLICKHOUSE_URL);
		database.put("driver", JdbcConstants.CLICKHOUSE_DRIVER);
		result.put(JdbcConstants.CLICKHOUSE, database);
		
		database=new HashMap();
		database.put("url", JdbcConstants.SQL_SERVER_URL);
		database.put("driver", JdbcConstants.SQL_SERVER_DRIVER);
		result.put(JdbcConstants.SQL_SERVER, database);
		
		
        return new ReturnT(result);
    }
	


	@ApiOperation(hidden = true, value = "测试连接", notes = "")
	@RequestMapping(path = "/runTest", method = RequestMethod.POST)
    public ReturnT runTest(@RequestBody DatasourceJdbcBO datasourceJdbcBO) throws Exception  {
		
		ReturnT returnT= null;
		datasourceJdbcService.runTest(datasourceJdbcBO);
		returnT= new ReturnT();
		


        return returnT;
    }
	
	@ApiOperation(hidden = true, value = "获取驱动", notes = "")
	@RequestMapping(path = "/getJdbcDriver", method = RequestMethod.POST)
    public ReturnT getJdbcDriver()  {
		

		
		Map map=new HashMap() ;
		
		map.put(JdbcConstants.SQL_SERVER, new Driver(JdbcConstants.SQL_SERVER_URL,JdbcConstants.SQL_SERVER_DRIVER_SQLJDBC4,""));
		map.put(JdbcConstants.MYSQL, new Driver(JdbcConstants.MYSQL_URL,JdbcConstants.MYSQL_DRIVER,""));
		map.put(JdbcConstants.CLICKHOUSE, new Driver(JdbcConstants.CLICKHOUSE_URL,JdbcConstants.CLICKHOUSE_DRIVER,""));
		map.put(JdbcConstants.ORACLE, new Driver(JdbcConstants.ORACLE_URL,JdbcConstants.ORACLE_DRIVER,""));
		map.put(JdbcConstants.POSTGRESQL, new Driver(JdbcConstants.POSTGRESQL_URL,JdbcConstants.POSTGRESQL_DRIVER,""));
		map.put(JdbcConstants.MONGODB, new Driver(JdbcConstants.MONGODB_URL,"",""));
		map.put(JdbcConstants.HIVE, new Driver(JdbcConstants.HIVE_URL,JdbcConstants.HIVE_DRIVER,""));
		map.put(JdbcConstants.HBASE20XSQL, new Driver(JdbcConstants.HBASE20XSQL_URL,JdbcConstants.HBASE20XSQL_DRIVER,""));
//		map.put(JdbcConstants.DB2, new Driver(JdbcConstants.DB2_URL,JdbcConstants.DB2_DRIVER,""));


        return new ReturnT(map);
    }
	
	@Data
	private static class Driver{
		private String url;
		private String driver;
		private String zookeeper;
		public Driver(String url, String driver,String zookeeper) {
			this.url=url;
			this.driver=driver;
			this.zookeeper=zookeeper;
		}
	}

}